package com.nirvana.item;

import com.nirvana.type.ItemType;
import com.nirvana.data.DbFieldInfo;

import javax.swing.*;

public class TableFieldItem extends AbstractItem {
    private final JTextField commentTextField = new JTextField();
    private final JTextField sqlTypeTextField = new JTextField();
    private final JTextField javaTypeTextField = new JTextField();

    private DbFieldInfo dbFieldInfo;

    @Override
    public void init() {
        super.init();
        int column = 2;
        propertyNameTextField.setEditable(false);
        sqlTypeTextField.setEditable(false);
        javaTypeTextField.setEditable(false);
        itemPanel.add(sqlTypeTextField, getConstraintsStr(column++));
        itemPanel.add(javaTypeTextField, getConstraintsStr(column++));
        itemPanel.add(commentTextField, getConstraintsStr(column++));
    }

    @Override
    protected ItemType getItemType() {
        return ItemType.TableFieldItem;
    }

    @Override
    public void copy(AbstractItem item) {}

    public void populate(DbFieldInfo dbFieldInfo) {
        this.dbFieldInfo = dbFieldInfo;
        propertyNameTextField.setText(dbFieldInfo.getColumnName());
        sqlTypeTextField.setText(dbFieldInfo.getSqlType());
        javaTypeTextField.setText(dbFieldInfo.getJavaType());
        commentTextField.setText(dbFieldInfo.getComment());
    }

    public DbFieldInfo getDbFieldInfo() {
        dbFieldInfo.setComment(commentTextField.getText());
        return dbFieldInfo;
    }
}
